package com.yunban.project.api.text_check.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.api.text_check.domain.CorrectionHistory;
import com.yunban.project.api.text_check.dto.CorrectionInfoReq;
import com.yunban.project.api.text_check.mapper.CorrectionHistoryMapper;
import com.yunban.project.api.text_check.service.CorrectionHistoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author: lijiajun
 * @date: 2024-11-19
 * @version: 1.0
 */
@Service
public class CorrectionHistoryServiceImpl extends ServiceImpl<CorrectionHistoryMapper, CorrectionHistory> implements CorrectionHistoryService {
    @Override
    public CorrectionHistory findByFileMd5(String md5) {
        LambdaQueryWrapper<CorrectionHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CorrectionHistory::getFileMd5, md5);
        return this.getOne(wrapper);
    }

    @Override
    public void asyncSaveCorrectionInfo(CorrectionInfoReq req) {
        // 在入库的时候也查一下是否已经存在，如果存在则不再入库（在调用这个异步方法之前也会进行检查的）
        CorrectionHistory r = this.findByFileMd5(req.getFileMd5());
        if (ObjectUtils.isEmpty(r)) {
            r = new CorrectionHistory();
            BeanUtils.copyProperties(req, r);
            this.save(r);
        }
    }
}
